package com.dfbz.controller;

import com.alibaba.excel.util.StringUtils;
import com.dfbz.entity.Article;
import com.dfbz.entity.Favorite;
import com.dfbz.entity.User;
import com.dfbz.pojo.Result;
import com.dfbz.pojo.StatusCode;
import com.dfbz.service.ArticleService;
import com.dfbz.service.FavoriteService;
import com.dfbz.service.UserService;
import com.dfbz.service.impl.ArticleServiceImpl;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("article")
public class ArticleController {

    @Autowired
    ArticleService articleService;

    @Autowired
    UserService userService;

    @Autowired
    FavoriteService favoriteService;

    /**
     * 条件分页查找文章列表
     * @param pageNum 页数
     * @param pageSize 页大小
     * @param map 条件map
     * @return 用户分页列表
     */
    @RequestMapping("findArticleByCondition/{pageNum}/{pageSize}")
    public Result findArticleByCondition(@PathVariable(value = "pageNum") Integer pageNum
            , @PathVariable(value = "pageSize") Integer pageSize
            , @RequestBody Map<String,String> map){

        //我需要一个文章列表信息
        PageInfo<Article> articlePageInfo = articleService.articleConditionPage(pageNum, pageSize, map);

        return new Result(true, StatusCode.OK,"查找成功",articlePageInfo);
    }

    /**
     *
     * @param map 内容map集合
     * @return
     */
    @RequestMapping("publishArticle")
    public Result publishArticle(@RequestBody Map<Object,Object> map){
        Article article = new Article();

        if (!map.isEmpty()&&map.containsKey("articleTitle")&&!StringUtils.isEmpty(map.get("articleTitle"))){

            if (map.containsKey("articleContext")&& !StringUtils.isEmpty(map.get("articleContext"))){
                Map<Object, Object> myInfo = (Map<Object, Object>) map.get("myInfo");
                //我的信息
                Object myId = myInfo.get("myId");
                Object myName = myInfo.get("myName");
                //页面内容
                String articleTitle =(String) map.get("articleTitle");
                String articleContext = (String) map.get("articleContext");

                article.setUserId((Integer) myId);
                article.setPublishRealName((String)myName);
                article.setTitle(articleTitle);
                article.setContent(articleContext);
                article.setPublishDate(new Date());

                articleService.insertSelective(article);
                return new Result(true,StatusCode.OK,"内容添加成功");
            }
            return new Result(true,StatusCode.OK,"内容不能为空");
        }
        return new Result(true,StatusCode.OK,"标题不能为空");
    }


    /**
     * 我的收藏页   条件分页逻辑
     * @param pageNum
     * @param pageSize
     * @param map value条件值
     * @return 结果集
     */
    @RequestMapping("titleByNameLimit/{pageNum}/{pageSize}")
    public Result titleByNameLimit(@PathVariable("pageNum") Integer pageNum
            , @PathVariable("pageSize")Integer pageSize
            , @RequestBody Map<String,Object> map
            , HttpServletRequest request){
        PageInfo<Article> collectionPageLimit=articleService.titleByNameLimit(pageNum,pageSize,map,request);

        return new Result(true,StatusCode.OK,"分页查询成功",collectionPageLimit);
    }


    /**
     * 找我的哪些朋友 也关注了这篇文章
     * @param articleId 文章id
     * @return 朋友集合
     */
    @RequestMapping("followArticleMyFriend/{articleId}")
    public Result followArticleMyFriend(@PathVariable(value = "articleId") String articleId,HttpServletRequest request){

        List<User> friendList=userService.followArticleMyFriend(articleId,request);

        return new Result(true,StatusCode.OK,"响应成功",friendList);
    }

    /**
     * 取消对该文章的关注
     * @param articleId  文章id
     * @param request 获取当前用户Id
     * @return 删除成功
     */
    @RequestMapping("unArticleCollect/{articleId}")
    public Result unArticleCollect(@PathVariable(value = "articleId") String articleId,HttpServletRequest request){

        HttpSession session = request.getSession();
        User loginUser = (User) session.getAttribute("loginUser");

        Favorite favorite = new Favorite();
        favorite.setaId(Integer.valueOf(articleId));
        favorite.setuId(loginUser.getId());

        int deleteNum = favoriteService.delete(favorite);
        return new Result(true,StatusCode.OK,"删除成功",deleteNum);
    }


}
